﻿var YXQCore = {
    Form: {},
    Dialog: {}, 
    Toggle: {},
    Cookie: {}
};

YXQCore.Toggle = {
    Tog: function (div) {
        $("#" + div).toggle();
    }
};
YXQCore.Cookie = {
    SetCookie: function (name, value,day) {
        var today = new Date();
        var expire = new Date();
        expire.setTime(today.getTime() + 3600000 * 24*day);
        document.cookie = escape(name + ";" + value);

    },
    GetCookie: function (v) {
        v = escape(v);
        var theCookie = "" + document.cookie;

        var ind = theCookie.indexOf(v); 
        if (ind == -1)
        { return ""; }
        return unescape(theCookie.substring(7, 11));

    }
};

YXQCore.Form = {

    PostPageSync: function (url, div, data) {
        if (data == undefined)
            data = {};
        $.ajax({
            type: "Post",
            url: url,
            data: data,
            cache: false,
            async: false,
            beforeSend: function (xhr) {
                $(div).html("<img src ='Images/loading.gif' alt='loading'  />");
            },
            success: function (response) {
                if (response.indexOf("HasLogOut") < 0) {
                    $(div).html(response);
                }
                else
                    alert("请重新登录！");
            },
            error: function (xml, status) {
                $(div).html(xml.responseText);
            }
        });
    },
    PostData: function (url, data, afterRender, onError) {
        if (data == undefined)
            data = {};
        $.ajax({
            type: "get",
            url: url,
            data: data,
            cache: false,
            beforeSend: function (xhr) {
                YXQCore.Form.ShowMaskLayer();
            },
            success: function (response) {

                if (afterRender != undefined) {
                    afterRender(response);
                }
                YXQCore.Form.CloseMaskLayer();

            },
            error: function (xml, status) {
                alert(xml.responseText);
                YXQCore.Form.CloseMaskLayer();
            }

        });
    },

    FileUpload: function (url,  data, afterRender) {
        $("#uploadify").uploadify({
            uploader: '../Infrastructure/uploadify.swf',
            script: url,
            cancelImg: '../Infrastructure/cancel.png',
            queueID: 'fileQueue',
            auto: false,
            multi: false,
            scriptData: data,
            onComplete: function (event, queueId, fileObj, response, data) {
                if (afterRender != undefined) {
                    afterRender(response.toString());
                }

            },
            onAllComplete: function (data) {
                if (afterRender != undefined) {
                    afterRender(data.toString());
                }
            },
            onError: function (event, queueId, fileObj, errorObj) {
                alert(errorObj.info);
            }
        });

    },

    ShowMaskLayer: function () {
        $("#Masklayer").css({ position: "absolute",
            height: Math.max(document.body.scrollHeight, document.documentElement.scrollHeight) + "px",
            width: Math.max(document.body.scrollWidth, document.documentElement.scrollWidth) + "px",
            filter: "alpha(opacity=50)",
            opacity: "0.5",
            top: "0",
            left: "0",
            index: "9000",
            background: "#CCCCCC",
            display: "block"
        });
        $("#Masklayer").show();
    },
    CloseMaskLayer: function () {
        $("#Masklayer").hide();
    },

    PostPage: function (url, div, data, afterRender) {
        if (data == undefined)
            data = {};
        $.ajax({
            type: "Post",
            url: url,
            data: data,
            cache: false,
            beforeSend: function (xhr) {
                $(div).html("<img src ='Images/loading.gif' alt='loading'  />");
                //增加遮罩层显示
                YXQCore.Form.ShowMaskLayer();
                //alert("ff");
            },
            success: function (response) {

                $(div).html(response);
                if (afterRender != undefined) {
                    afterRender(response);
                }
                //关闭遮罩层
                YXQCore.Form.CloseMaskLayer();

            },
            error: function (xml, status) {
                $(div).html(xml.responseText);
                YXQCore.Form.CloseMaskLayer();
            }
        });
    },
    GetDivFormData: function (div) {
        var data = {};
        div.find("[DataField]").each(function (i) {
            data[$(this).attr("DataField")] = YXQCore.Form.GetControlData($(this));
        });
        return data;
    },
    SetDivFormData: function (div, jsonData) {
        div.find("[DataField]").each(function (i) {
            var dataField = $(this).attr("DataField");
            if (dataField != undefined) {
                YXQCore.Form.SetControlData($(this), jsonData[dataField]);
            }
        });
    },
    SetControlData: function (control, data) {
        switch ($(control).attr("tagName")) {
            case "INPUT":
                {
                    data += $(control).val(data);
                }
                break;
            case "SPAN":
                {
                    data += $(control).text(data);
                }
                break;
            default:
                data += $(control).val(data);
                break;
        }

    },
    GetControlData: function (control) {
        var data = "";
        switch ($(control).attr("tagName")) {
            case "TEXTAREA":
            case "INPUT":
                {
                    if ($(control).attr("type") == "checkbox") {
                        data += $(control).attr("checked");
                    }
                    else
                        data += $(control).val();

                }
                break;
            case "SPAN":
                {
                    data += $(control).text();
                }
                break;
            case "SELECT":
                {
                    data += $(control).val();
                }
                break; default:
                data += $(control).val();
                break;
        }
        return YXQCore.Form.ConvertToJson(data);
    },
    ConvertToJson: function (s) {
        s = s.replace(/(\\|\"|\')/g, "\\$1")
                    .replace(/\n|\r|\t/g,
                    function () {
                        var a = arguments[0];
                        return (a == '\n') ? '\\n' :
                           (a == '\r') ? '\\r' :
                           (a == '\t') ? '\\t' : ""
                    });
        return s;
    },
    ConverArryToString: function (s) {
        var newstring = '';
        if (s.length > 0) {
            for (var i = 0; i < s.length; i++) {
                newstring = newstring + s[i] + ",";
            }
        } else {
            newstring = s;
        }
        return newstring;
    },
   
    GetHfControlValue: function (o) {
        return $("#" + o).val();
    },
    SetHfControlValue: function (o, rowid) {
        $("#" + o).val(rowid);
    },
    QueryNormalForm: function (code, func) {

    }

}; 

  
Date.prototype.format = function(formatter) {
    if (!formatter || formatter == "") {
        formatter = "yyyy-MM-dd";
    }
    var year = this.getYear().toString();
    var month = (this.getMonth() + 1).toString();
    var day = this.getDate().toString();
    var yearMarker = formatter.replace(/[^y|Y]/g, '');
    if (yearMarker.length == 2) {
        year = year.substring(2, 4);
    }
    var monthMarker = formatter.replace(/[^m|M]/g, '');
    if (monthMarker.length > 1) {
        if (month.length == 1) {
            month = "0" + month;
        }
    }
    var dayMarker = formatter.replace(/[^d]/g, '');
    if (dayMarker.length > 1) {
        if (day.length == 1) {
            day = "0" + day;
        }
    }
    return formatter.replace(yearMarker, year).replace(monthMarker, month).replace(dayMarker, day);
}
Date.parseString = function(dateString, formatter) {
    var today = new Date();
    if (!dateString || dateString == "") {
        return today;
    }
    if (!formatter || formatter == "") {
        formatter = "yyyy-MM-dd";
    }
    var yearMarker = formatter.replace(/[^y|Y]/g, '');
    var monthMarker = formatter.replace(/[^m|M]/g, '');
    var dayMarker = formatter.replace(/[^d]/g, '');
    var yearPosition = formatter.indexOf(yearMarker);
    var yearLength = yearMarker.length;
    var year = dateString.substring(yearPosition, yearPosition + yearLength) * 1;
    if (yearLength == 2) {
        if (year < 50) {
            year += 2000;
        }
        else {
            year += 1900;
        }
    }
    var monthPosition = formatter.indexOf(monthMarker);
    var month = dateString.substring(monthPosition, monthPosition + monthMarker.length) * 1 - 1;
    var dayPosition = formatter.indexOf(dayMarker);
    var day = dateString.substring(dayPosition, dayPosition + dayMarker.length) * 1;
    return new Date(year, month, day);
}



 

YXQCore.Dialog = {
    DefaultDialog: null,
    DefaultOptions: {
        autoOpen: false,
        modal: true,
        bgiframe: true,
        height: 400,
        width: 750,
        title: "",
        close: function(event, ui) {
            //YXQCore.Dialog.DefaultDialog.empty();
        }
    },
    InitDialog: function() {

        $("#dialogSystem").dialog(YXQCore.Dialog.DefaultOptions);
        YXQCore.Dialog.DefaultDialog = $("#dialogSystem");
    },
    OpenDialog: function(options) {
        if (options != undefined) {
            $.each(YXQCore.Dialog.DefaultOptions, function(key, value) {
                if (options[key] == null)
                    options[key] = value;
            });
            YXQCore.Dialog.DefaultDialog.dialog("option", options);
        }
        YXQCore.Dialog.DefaultDialog.dialog("open");
    },
    CloseDialog: function() {
        YXQCore.Dialog.DefaultDialog.dialog("close");
    }




};


//时间格式化
Date.prototype.format = function (format) {
    /*
    * eg:format="yyyy-MM-dd hh:mm:ss";
    */
    if (!format) {
        format = "yyyy-MM-dd";
    }

    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};